/*
 * InfoDialog.java
 *
 * Created on 13. Februar 2005, 10:53
 */

package de.gidoo.owl;

import java.io.IOException;
import javax.swing.event.*;
import java.util.*;
import java.io.File;
import java.net.URL;

/**
 * Displays infos about the program, the licence and the link to the homepage.
 * @author Thomas Krause
 */
public class InfoDialog extends javax.swing.JDialog {
  
  private ResourceBundle i18n;
  
  private ErrorDialog errorDlg;
  
  private LinkedList<URL> backURL;
  private LinkedList<URL> forwardURL;
  
  /** Creates new form InfoDialog */
  public InfoDialog(java.awt.Frame parent, boolean modal, ResourceBundle i18n) {
      super(parent, modal);
      
      this.i18n = i18n;
      errorDlg = new ErrorDialog(parent, modal, this.i18n);
      
      backURL = new LinkedList<URL>();
      forwardURL = new LinkedList<URL>();
      
      initComponents();
        
  }
        
  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
  private void initComponents() {
    btOk = new javax.swing.JButton();
    tabPanel = new javax.swing.JTabbedPane();
    panGeneral = new javax.swing.JPanel();
    lblTitle = new javax.swing.JLabel();
    lblText = new javax.swing.JLabel();
    lblIcon = new javax.swing.JLabel();
    panLicence = new javax.swing.JPanel();
    scrollLicence = new javax.swing.JScrollPane();
    lblLicence = new javax.swing.JEditorPane();
    panUsage = new javax.swing.JPanel();
    btBack = new javax.swing.JButton();
    btForward = new javax.swing.JButton();
    btHome = new javax.swing.JButton();
    scrollUsage = new javax.swing.JScrollPane();
    lblUsage = new javax.swing.JEditorPane();

    getContentPane().setLayout(null);

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle(java.util.ResourceBundle.getBundle("de/gidoo/owl/OwlReader").getString("infoTitle"));
    setResizable(false);
    btOk.setFont(new java.awt.Font("Dialog", 0, 12));
    btOk.setMnemonic(i18n.getString("btCloseChar").charAt(0));
    btOk.setText(i18n.getString("btClose"));
    btOk.setName("btOk");
    btOk.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        okClicked(evt);
      }
    });

    getContentPane().add(btOk);
    btOk.setBounds(0, 320, 390, 25);

    tabPanel.setFont(new java.awt.Font("Dialog", 0, 12));
    panGeneral.setLayout(null);

    lblTitle.setFont(new java.awt.Font("Dialog", 1, 18));
    lblTitle.setForeground(new java.awt.Color(0, 0, 153));
    lblTitle.setText(i18n.getString("infoTitleGeneral"));
    panGeneral.add(lblTitle);
    lblTitle.setBounds(70, 20, 120, 30);

    lblText.setFont(new java.awt.Font("Dialog", 0, 12));
    lblText.setText(i18n.getString("infoTextGeneral"));
    lblText.setVerticalAlignment(javax.swing.SwingConstants.TOP);
    panGeneral.add(lblText);
    lblText.setBounds(10, 70, 370, 210);

    lblIcon.setIcon(new javax.swing.ImageIcon("res/icon48.png"));
    panGeneral.add(lblIcon);
    lblIcon.setBounds(10, 10, 48, 48);

    tabPanel.addTab(i18n.getString("infoTabGeneral"), panGeneral);

    panLicence.setLayout(new java.awt.GridLayout(1, 0));

    panLicence.addComponentListener(new java.awt.event.ComponentAdapter() {
      public void componentShown(java.awt.event.ComponentEvent evt) {
        licenceReset(evt);
      }
    });

    scrollLicence.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    lblLicence.setContentType("text/html");
    lblLicence.setEditable(false);
    lblLicence.setText(i18n.getString("infoLicenceText"));
    lblLicence.setContentType("text/html");
    lblLicence.addHyperlinkListener(new javax.swing.event.HyperlinkListener() {
      public void hyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {
        hyperLinkUpdate(evt);
      }
    });

    scrollLicence.setViewportView(lblLicence);

    panLicence.add(scrollLicence);

    tabPanel.addTab(i18n.getString("infoTabLicence"), panLicence);

    panUsage.setLayout(null);

    panUsage.addComponentListener(new java.awt.event.ComponentAdapter() {
      public void componentShown(java.awt.event.ComponentEvent evt) {
        resetUsage(evt);
      }
    });

    btBack.setFont(new java.awt.Font("Dialog", 0, 12));
    btBack.setIcon(new javax.swing.ImageIcon("res/Back24.gif"));
    btBack.setActionCommand("back");
    btBack.setEnabled(false);
    btBack.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        backClicked(evt);
      }
    });

    panUsage.add(btBack);
    btBack.setBounds(10, 0, 30, 30);

    btForward.setFont(new java.awt.Font("Dialog", 0, 12));
    btForward.setIcon(new javax.swing.ImageIcon("res/Forward24.gif"));
    btForward.setActionCommand("forward");
    btForward.setEnabled(false);
    btForward.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        forwardClicked(evt);
      }
    });

    panUsage.add(btForward);
    btForward.setBounds(50, 0, 30, 30);

    btHome.setFont(new java.awt.Font("Dialog", 0, 12));
    btHome.setIcon(new javax.swing.ImageIcon("res/Home24.gif"));
    btHome.setActionCommand("home");
    btHome.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        homeClicked(evt);
      }
    });

    panUsage.add(btHome);
    btHome.setBounds(90, 0, 30, 30);

    lblUsage.setEditable(false);
    lblUsage.setContentType("text/html");
    lblUsage.addHyperlinkListener(new javax.swing.event.HyperlinkListener() {
      public void hyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {
        usageHyperlink(evt);
      }
    });

    scrollUsage.setViewportView(lblUsage);

    panUsage.add(scrollUsage);
    scrollUsage.setBounds(0, 30, 390, 260);

    tabPanel.addTab(i18n.getString("infoTabHelp"), panUsage);

    getContentPane().add(tabPanel);
    tabPanel.setBounds(0, 0, 390, 310);

    java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    setBounds((screenSize.width-401)/2, (screenSize.height-381)/2, 401, 381);
  }
  // </editor-fold>//GEN-END:initComponents

  /** Handler for clicks on the button btHome  */
  private void homeClicked(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_homeClicked
    
    try
    {
      File f = new File("docs/index.html");
      goTo(f.toURL(), true);
    }
    catch(Exception e)
    {
      errorDlg.showError(i18n.getString("errorInfoLoadUsage"), e);
    }
    
  }//GEN-LAST:event_homeClicked

  /** Handler for clicks on the button btForward  */
  private void forwardClicked(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_forwardClicked
  
    goTo(forwardURL.poll(), true);
  }//GEN-LAST:event_forwardClicked

  /** Handler for clicks on the button btBack  */
  private void backClicked(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backClicked
    
    forwardURL.add(0, lblUsage.getPage());
    goTo(backURL.poll(), false);
  }//GEN-LAST:event_backClicked

  /** Handler for clicks in hyperlinks in the usage-panel */
  private void usageHyperlink(javax.swing.event.HyperlinkEvent evt) {//GEN-FIRST:event_usageHyperlink
    if(evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
    {
      // follow the link
      goTo(evt.getURL(), true);
    }
  }//GEN-LAST:event_usageHyperlink

  /**
   * Load the starting page of the help
   */
  private void resetUsage(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_resetUsage
    try
    {
      File f = new File("docs/index.html");
      goTo(f.toURL(), false);
    }
    catch(Exception e)
    {
      errorDlg.showError(i18n.getString("errorInfoLoadUsage"), e);
    }
  }//GEN-LAST:event_resetUsage

  /**
   * Shows the full text of an abbrivation if mouse is over it.
   */
    private void hyperLinkUpdate(javax.swing.event.HyperlinkEvent evt) {//GEN-FIRST:event_hyperLinkUpdate

      String address = "";
      String title = "";
      java.awt.Graphics gc;
      java.awt.Point mousePos;
        
      // look what kind of action
      
      HyperlinkEvent.EventType type = evt.getEventType();
      if(type == HyperlinkEvent.EventType.ENTERED)
      {
        // just ENTERED
        address = evt.getDescription();
                   
        if(address.startsWith("?abbr:"))
        {
          title = address.substring(6);
              
          gc = getGraphics();
          mousePos = getMousePosition();
            
          update(gc);
          java.awt.Color oldColor = gc.getColor();
          java.awt.Color newColor = new java.awt.Color(255, 200, 0);
            
          // inner rectangle
          gc.setColor(newColor);
          java.awt.FontMetrics fm = gc.getFontMetrics();
            
          int rectX = mousePos.x;
          int rectY = mousePos.y - fm.getHeight() + fm.getDescent();
          int rectHeight = fm.getHeight();
          int rectWidth = fm.stringWidth(title);
            
          gc.fillRect(rectX, rectY, rectWidth, rectHeight);
            
          // bounds
          newColor = new java.awt.Color(0, 0, 0);
          gc.setColor(newColor);
          gc.drawRect(rectX - 1, rectY - 1, rectWidth + 1, rectHeight + 1);
            
          // text
          newColor = new java.awt.Color(0, 0, 0);
          gc.setColor(newColor);            
          gc.drawString(title, mousePos.x, mousePos.y);
          gc.setColor(oldColor);
            
        }
      }
      else if(type == HyperlinkEvent.EventType.EXITED)
      {
        // just EXITED
        address = evt.getDescription();
        if(address.startsWith("?abbr:"))
        {
           gc = getGraphics();
           update(gc);
        }
      }
        
    }//GEN-LAST:event_hyperLinkUpdate
    
    /**
     * Set the page to the given URL
     * @param page the page to open
     * @param addToBack wether the current page shall be added to the history
     */
    private void goTo(URL page, boolean addToBack)
    {
      try
      {
        if(addToBack)
        {
          backURL.add(0, lblUsage.getPage());
        }
        
        if(backURL.size() > 0)
        {
          btBack.setEnabled(true);
        }
        else
        {
          btBack.setEnabled(false);
        }
        
        if(forwardURL.size() > 0)
        {
          btForward.setEnabled(true);
        }
        else
        {
          btForward.setEnabled(false);
        }
        
        lblUsage.setPage(page);

        
      }
      catch(Exception e)
      {
        if(addToBack)
        {
          backURL.poll();
        }
        
        errorDlg.showError(i18n.getString("errorInfoLoadUsage"), e);
      }
    }
    
    /**
     * Scroll to the begin of the licence.
     */
    private void licenceReset(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_licenceReset
        lblLicence.scrollToReference("start");
        
    }//GEN-LAST:event_licenceReset

    /**
     * User requested to close the window by clicking on "ok".
     */
    private void okClicked(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okClicked
      this.setVisible(false);
    }//GEN-LAST:event_okClicked
    
    
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton btBack;
  private javax.swing.JButton btForward;
  private javax.swing.JButton btHome;
  private javax.swing.JButton btOk;
  private javax.swing.JLabel lblIcon;
  private javax.swing.JEditorPane lblLicence;
  private javax.swing.JLabel lblText;
  private javax.swing.JLabel lblTitle;
  private javax.swing.JEditorPane lblUsage;
  private javax.swing.JPanel panGeneral;
  private javax.swing.JPanel panLicence;
  private javax.swing.JPanel panUsage;
  private javax.swing.JScrollPane scrollLicence;
  private javax.swing.JScrollPane scrollUsage;
  private javax.swing.JTabbedPane tabPanel;
  // End of variables declaration//GEN-END:variables
    
}
